let Div = document.createElement("div")
Div.className = "dName"
document.body.appendChild(Div)

let dragging = false
let lastX
let lastY
Div.onmousedown = (e) => {
  lastX = e.clientX
  lastY = e.clientY
  dragging = true
}
Div.onmouseup = () => {
  dragging = false
}

document.onmousemove = (event) => {
  const {clientWidth, clientHeight} = document.getElementsByClassName("dName")[0]
  if (dragging) {
    let deltaX = event.clientX - lastX
    let deltaY = event.clientY - lastY
    let top = parseInt(Div.style.top) || 0
    let left = parseInt(Div.style.left) || 0
    let resultX = left + deltaX
    let resultY = top + deltaY
    resultX < 0 && (resultX = 0)
    resultY < 0 && (resultY = 0)

    Div.style.top = resultY + "px"
    Div.style.left = resultX + "px"
    lastX = event.clientX
    lastY = event.clientY
  }
}
